package cn.enjoy.dynamic.datasource.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * 动态数据源 AbstractRoutingDataSource，spring提供基于查找的key（look key）来动态调用各种数据源之一
 *
 * @author K
 * @date 2020/12/14 15:54
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    private final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);

    @Override
    protected Object determineCurrentLookupKey() {
        String datasource = DynamicContextHolder.peek();
        if (DynamicContextHolder.peek() == null) {
            datasource = "默认";
        }
        logger.info("Used datasource is " + datasource);
        return datasource;
    }
}
